package cn.yeyiu.netonlinevideo.util;

import android.app.Activity;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.InfoData;
import cn.yeyiu.netonlinevideo.bean.PageData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.ThreadUtil;
import cn.yeyiu.util.bean.MediaData;

/**
 * 视频获取封装工具
 */

public class DataServer {

    private SourceObserver.TypesChangeListener typesChangeListener;
    private SourceObserver.DatasChangeListener datasChangeListener;

    private final String mCode;
    private final Parser parser;

    private final Map<String, PageData> data;

    private final Activity mActivity;

    public DataServer(Activity activity, String code) {
        mActivity = activity;
        mCode = code;
        parser = ParserFactory.getParser(mCode);
        data = new HashMap<>();
    }


    public void setTypesChangeListener(SourceObserver.TypesChangeListener typesChangeListener) {
        this.typesChangeListener = typesChangeListener;
    }

    public void setDatasChangeListener(SourceObserver.DatasChangeListener datasChangeListener) {
        this.datasChangeListener = datasChangeListener;
    }

    public void postTypeList() {
        if (typesChangeListener == null) {
            AndroidUtil.toast("未设置事件: typeChange");
            return;
        }
        List<TypeData> typeList = SourceUtil.dataModel.getTypeList(mCode);
        if (typeList != null && typeList.size() > 0) {
            typesChangeListener.onTypesChange(typeList);

        } else {
            ThreadUtil.run(() -> {
                List<TypeData> typeData = parser.getTypeData();
                if (typeData != null) {
                    // 更新持久化数据
                    SourceUtil.dataModel.updateTypeList(mCode, typeData);
                    // 回调
                    mActivity.runOnUiThread(() -> {
                        typesChangeListener.onTypesChange(typeData);
                    });
                } else {
                    AndroidUtil.toast(String.format(Locale.CHINA, "类型数据获取失败 \n平台: %s", mCode));
                }
            });
        }
    }

    public void postOnePageDataList(String type, int index) {
        if (datasChangeListener == null) {
            AndroidUtil.toast("未设置事件: dataChange");
            return;
        }
        List<MediaData> dataList = SourceUtil.dataModel.getIndexData(mCode, type, index);
        if (dataList != null && dataList.size() > 0) {

            PageData pageData = SourceUtil.dataModel.getPage(mCode, type);
            datasChangeListener.onDatasChange(new InfoData(mCode, type, index, pageData.maxPage, dataList));
        } else {
            ThreadUtil.run(() -> {
                InfoData infoData = parser.getInfoData(type, index);
                if (infoData == null) {
                    AndroidUtil.toast(String.format(Locale.CHINA, "数据获取失败 \n平台: %s\n类型: %s\n分页: %d", mCode, type, index + 1));
                } else {
                    PageData pageData = data.get(type);
                    if (pageData == null) {
                        pageData = new PageData(mCode, type);
                    }
                    pageData.curPage = infoData.index;
                    pageData.maxPage = infoData.maxIndex;
                    data.put(type, pageData);

                    // 更新持久化数据
                    SourceUtil.dataModel.updateData(infoData);
                    // 回调
                    mActivity.runOnUiThread(() -> {
                        datasChangeListener.onDatasChange(infoData);
                    });
                }
            });
        }
    }

    public void postFirstPageData(String type) {
        postOnePageDataList(type, 0);
    }

    public boolean hasInit(String type) {
        return data.containsKey(type);
    }

    public void nextPage(String type) {
        changePage(true, type);
    }

    public void prePage(String type) {
        changePage(false, type);
    }

    private void changePage(boolean next, String type) {
        AndroidUtil.toastWait();
        PageData pageData = data.get(type);
        if (pageData == null || pageData.maxPage == 0) {
            return;
        }
        pageData.curPage = pageData.curPage + 1 > pageData.maxPage ? 0 : pageData.curPage + 1;
        postOnePageDataList(type, pageData.curPage);
    }

    public PageData getPageData(String type) {
        return data.get(type);
    }

    public int getCurPage(String type) {
        PageData pageData = data.get(type);
        if (pageData == null) {
            return 1;
        } else {
            return pageData.curPage + 1;
        }
    }

}
